<?php
include_once("./lib/timespan_settings.php");
include_once(dirname(__FILE__) . "/libs/nms_html_scripts.php");
include_once(dirname(__FILE__) . "/libs/nms_html_views.php");
include_once(dirname(__FILE__) . "/libs/nms_time_system.php");
include_once(dirname(__FILE__) . "/model/nms_report.php");
function draw_port_report(){
?>
<script type="text/javascript" src="./libs/jquery.min.js"></script>
<script type="text/javascript" src="./libs/jquery-latest.js"></script>
<script type="text/javascript" src="./libs/jquery.highlight-4.js"></script>
<script src="style/chosen.jquery.js" type="text/javascript"></script>
<script type="text/javascript">
	function getQueryStrings() { 
		  var assoc  = {};
		  var decode = function (s) { return decodeURIComponent(s.replace(/\+/g, " ")); };
		  var queryString = location.search.substring(1); 
		  var keyValues = queryString.split('&'); 
	
		  for(var i in keyValues) { 
		    var key = keyValues[i].split('=');
		    if (key.length > 1) {
		      assoc[decode(key[0])] = decode(key[1]);
		    }
		  } 
		  return assoc; 
		}	
	
</script>

<!-- Script call  device filter, search ---------------> 	 
<script>
function highlight(container,what,spanClass) {
    var content = container.innerHTML,
        pattern = new RegExp('(>[^<.]*)(' + what + ')([^<.]*)','g'),
        replaceWith = '$1<span ' + ( spanClass ? 'class="' + spanClass + '"' : '' ) + '">$2</span>$3',
        highlighted = content.replace(pattern,replaceWith);
    return (container.innerHTML = highlighted) !== content;
}
	$(document).ready(function() {
		$("#id_port").change(function(){
		 	//get current selected value of combobox
		 	var port =$(this).val();
		 	var hport =  $("#id_host_report").val();
		 	var presents =$("#id_presets_port").val();
		 	//build query string
		 	var queryString = "run=report_device&tab=port&hport="+hport+"&port="+port+"&presets="+presents;
		 	document.location.href = "?"+queryString;
		});
		$("#id_host_report").change(function(){
		 	//get current selected value of combobox
		 	var hport = $(this).val();
		 	var port =$("#id_port").val();
		 	var presents =$("#id_presets_port").val();
		 	//build query string
		 	var queryString = "run=report_device&tab=port&hport="+hport+"&port="+port+"&presets="+presents;
		 	document.location.href = "?"+queryString;
		});
		$("#id_host").change(function(){
		 	//get current selected value of combobox
		 	var id = $(this).val();
		 	var hport = $("#id_host_report").val();
		 	var port =$("#id_port").val();
		 	var presents =$("#id_presets_port").val();
		 	//build query string
		 	var queryString = "run=report_device&tab=port&hport="+hport+"&port="+port+"&id_hs="+id+"&presets="+presents;
		 	document.location.href = "?"+queryString;
		});
		$("#id_presets_port").change(function(){
		 	//get current selected value of combobox
		 	var presents = $(this).val();
		 	var hport = $("#id_host_report").val();
		 	var filter =$("#id_filter_port").val();
		 	var port =$("#id_port").val();
		 	//build query string
		 	var queryString = "run=report_device&tab=port&hport="+hport+"&port="+port+"&presets="+presents;
		 	document.location.href = "?"+queryString;
		});
		$("#btnGo1"). click(function (){
		 	//get current selected value of combobox
		 	//var LimitValue = $("id_limit_report").val();
		 	var hport = $("#id_host_report").val();
		 	var presents =$("#id_presets_port").val();
		 	var date_from = $("#date").val();
		 	var date_to = $("#date_2").val();
		 	var port =$("#id_port").val();
		 	//build query string
		 	var queryString = "run=report_device&tab=port&hport="+hport+"&port="+port+"&presets="+presents+"&datefrom="+date_from+"&dateto="+date_to+"&export=port";
		 	document.location.href = "?"+queryString;
		});
	/*Button Refresh change event*/
		 $("#btnRefresh"). click(function (){
				window.location.reload();
				history.go(0);
				window.location.href = window.location.href;
		 });
	});
</script>
<script>
	function reLoad1(){
		var hport = $("#id_host_report").val();
	 	var port =$("#id_port").val();
	 	//build query string
		var queryString = "run=report_device&tab=port&hport="+hport+"&port="+port;
		window.location.href = "?"+queryString;
		location.href ="?"+queryString;
	}
</script>
<?php 
	nms_port_report_header();
	if (empty($_REQUEST['hport'])) {
		$host_report="";
	}
	else{
		$host_report =  $_REQUEST['hport'];
	}
	if (empty($_REQUEST['id_hs'])) {
		$host_id="";
	}
	else{
		$host_id =  $_REQUEST['id_hs'];
	}
	if (empty($_REQUEST['presets'])) {
		$presets="";
	}
	else{
		$presets =  $_REQUEST['presets'];
	}
	if (empty($_REQUEST['port'])) {
		$port="";
	}
	else{
		$port =  $_REQUEST['port'];
	}
	if(isset($_REQUEST['hport']) ){
		if($_REQUEST['hport']=="select"){	
		$url= "nms.php?run=report_device&tab=port&hport=".$host_report."&port=".$port."&id_hs=".$host_id."&presets=".$presets;
			nms_port_report_body($url);
		}
	}
	nms_port_report_fotter();
	?>
		<script type="text/javascript">
			$("#id_port").val('<?php echo $port;?>');
			$("#id_host_report").val('<?php echo $host_report;?>');
			$("#id_host").val('<?php echo $host_id;?>');
			$("#id_presets_port").val('<?php echo $presets;?>');
		</script>
	<?php 
	if(isset($_POST['btnAdd'])){
		if($_REQUEST['hport']=="select"){
			$id = $_REQUEST['id_hs'];
			$name ="port_report";
		if(check_insert_host($id,$name)==true){
			echo'	
				<script>
					alert("Host existence !!!");
				</script>';
			}
			else {
				insert_host($id,$name);
			}
			echo '
		  		<script>
		  			reLoad1();
		  		</script>
		  		';
		}
	}
		if(isset($_REQUEST['action'])){
			if($_REQUEST['action']=='delete'){
				$id = $_REQUEST['id'];
				$name ="port_report";
				delete_one_host_report($id,$name);
				echo '
		  		<script>
		  			reLoad1();
		  		</script>
		  		';
			}
		}
}

function nms_port_report_header(){
	?>
	<div id="id_report_1" >
	<div id="id_master_header">
				<h2>  </h2>
	</div>
	<div>&nbsp;</div>
	<div id="id_report_header">
		<div id="id_report_header1">
			<strong>Port:</strong>
	  					<select  onchange="val()" id="id_port">
	  						<option value=""></option>
							<?php
								$port = db_fetch_assoc("select SUBSTRING(port_name,1,2) as port_value from plugin_nms_translog GROUP BY port_value");
								if (sizeof($port) > 0) {
									foreach($port as $set) {
										echo "<option value='" . $set['port_value'] . "'"; echo ">" . htmlspecialchars($set['port_value']) . "</option>\n";
									}
								}
							?>
						</select>
			<strong>Device:</strong>
				<select  onchange="val()" id="id_host_report">
							<option value=""></option>
		    				<option value="all">All Host</option>
		    				<option value="select">Select Host</option>
		     	</select>
			
		</div>
		<div class="vide"></div>
	</div>
<?php 
}
function nms_port_report_body($url){
	?>
			<div id="id_master_header">
				<h2>  </h2>
			</div>
			<div id ="id_report_body">
			<form action="<?php echo $url?>" method ="post"enctype="multipart/form-data" >
				<div id="id_report_body1">
					<?php if($_REQUEST['hport']=="select"){ ?>
					<table>
						<tr>
						<td>
						<strong>Device:</strong>
						</td>
					
						<td>
						<select  onchange="val()" id="id_host">
		    				<?php
	     					 $hosts = db_fetch_assoc("select id,CONCAT_WS('',description,' (',hostname,')') as name from host where description !='Localhost' order by description,hostname");  $result3 = db_fetch_assoc($sql54);
	     					 if (sizeof($hosts) > 0) {
						     foreach($hosts as $set) {
						     echo "<option value='" . $set["id"] . "'"; 
						     if(isset($_REQUEST["id_hs"])){if($_REQUEST["id_hs"] == $set["id"]){echo "selected";}};
						     echo ">" . htmlspecialchars($set["name"]) . "</option>\n";
						     }
						     }
	   						?>
		     			</select>
		     			<script type="text/javascript">
						$("#id_host").chosen({
							search_contains: true,
							no_results_text:"Oops, nothing found!"});
						$("#id_host").on('change',function(){
						 	//get current selected value of combobox
							var id = $(this).val();
						 	var hport = $("#id_host_report").val();
						 	var port =$("#id_port").val();
						 	var presents =$("#id_presets_port").val();
						 	//build query string
						 	var queryString = "run=report_device&tab=port&hport="+hport+"&port="+port+"&id_hs="+id+"&presets="+presents;
						 	document.location.href = "?"+queryString;
						});
 					 </script>
		     			</td>
		     			<td nowrap style='white-space: nowrap;'>
						&nbsp;<input type="submit" name="btnAdd" id="btnAdd"  value="Add" />
						</td>
						</tr>
					</table>
					<table  width="100%">
									<tr class="cls_master_table_title" >
										<th width="5%" scope="col">Id</th>
										<th width="30%" scope="col">Host_name</th>
										<th width="40%" scope="col">Description</th>
										<th width="1%" scope="col"></th>
									</tr>
									<?php 
									$data_host = db_fetch_assoc('SELECT host_id , hostname,description from plugin_nms_report_host a ,host b
																where a.host_id =b.id and a.report_group ="port_report" order by host_id');
									$stt =0;
									foreach ($data_host as $set){
									$stt++;
									?>
									<tr <?php if($stt%2==0) {echo 'class="xanhlot"';}
				 						else {echo 'class="xanhhong"';} ?>> 
										<td align="center">&nbsp;<?php echo $set['host_id']?></td>
										<td align="center">&nbsp;<?php echo $set['hostname']?></td>
										<td align="center">&nbsp;<?php echo $set['description']?></td>
										<td align="center"><a href="<?php echo $url?>&action=delete&id=<?php echo $set['host_id'];?>"onclick="return confirm('Are you sure you want to delete?')"><?php $stt;echo'Delete';?></a></td>
									</tr>
									<?php 
									}?>
					</table>
					<?php }?>
				</div>
				<div class="vide"></div>
				</form>
			</div>
	<?php 
}
function nms_port_report_fotter(){
	?>
			<div id="id_master_header">
				<h2>  </h2>
			</div>
		<div id="id_master_center_top">
			<div id="id_master_center_top1">
			<table border="0">
    			<tr>
    				<td>&nbsp;</td>
					<td> <strong id="">Chosse date:</strong>
					</td>
					<td>
							<select onchange="val()"  id="id_presets_port">
								<option value=""></option>
								<?php
									draw_time_filter();
								?>
								</select>
					</td>
	  				<!-- Button Calendar -->
	  					<?php 
	  					if (empty($_REQUEST['presets'])) {
	  						$_SESSION["current_date_port"] =date("Y-m-d H:i", strtotime('-1 days'));;
							$_SESSION["current_date_port_2"] =date('Y-m-d H:i');
	  					}
	  					else{
	  						$id = $_REQUEST['presets'];
	  						$_SESSION["current_date_port"] =nms_time_from($id);
	  						$_SESSION["current_date_port_2"]= nms_time_to($id);
	  						
	  					}
						?>
						<td nowrap style='white-space: nowrap;'>
							<input type='text' name='date' id='date' title='Graph Begin Timestamp' size='15' value='<?php print (isset($_SESSION["current_date_port"]) ? $_SESSION["current_date_port"] : "");?>'>
						</td>
						<td nowrap style='white-space: nowrap;'>
							&nbsp;<input type='image' src='images/iconCalendar.gif' align='middle' alt='Start date selector' title='Start date selector' onclick="return showCalendar('date');">
						</td>
						<td nowrap style='white-space: nowrap;'>
							&nbsp;<strong>To:</strong>&nbsp;
						</td>
						<td nowrap style='white-space: nowrap;'>
							<input type='text' name='date_2' id='date_2' title='Graph End Timestamp' size='15' value='<?php print (isset($_SESSION["current_date_port_2"]) ? $_SESSION["current_date_port_2"] : "");?>'>
						</td>
						<td nowrap style='white-space: nowrap;'>
							&nbsp;<input type='image' src='images/iconCalendar.gif' align='middle' alt='End date selector' title='End date selector' onclick="return showCalendar('date_2');">
						</td>
		  				<td nowrap style='white-space: nowrap;'>
							&nbsp;<input id="btnGo1" type="button"  value="Export" title="Export to a file">
						</td>
						<td nowrap style='white-space: nowrap;'>
							&nbsp;<input id="btnClear1" type="button" value="Clear" onclick="location.href='';"/>
						</td>
						<td>
				  	 		<h4>Refresh time:</h4>
				  	 	</td>
				  	 	<td>
				  	 		<h4><div id ="countdown_time2"></div></h3>
				  	 	</td> 		
			    </tr>
    		</table> 
    		</div>
    	</div>
	<?php 
	html_alarm_script_time();
}
?>